package com.rayo.accesscontrollercopy.model.db.dao;

import com.litesuits.orm.db.assit.QueryBuilder;
import com.rayo.accesscontrollercopy.model.db.DbHelp;
import com.rayo.accesscontrollercopy.model.db.bean.CardTimezoneData;
import com.rayo.accesscontrollercopy.model.db.bean.KeyData;

import java.util.List;

/**
 * Created by lyc-pc on 17/5/12.
 */

public class KeyDataImp implements KeyDataDao {
    @Override
    public KeyData getKeyDataByPos(int pos,int adress) {
        QueryBuilder qb = new QueryBuilder(KeyData.class).whereEquals("pos", pos)
                .whereAppendAnd().whereEquals("keybox", adress);
        List<KeyData> list = DbHelp.liteOrm.query(qb);
        if (list.size() > 0)
            return list.get(0);
        return null;
    }

    @Override
    public void addKeyData(KeyData keyData) {
        DbHelp.insert(keyData);
    }

    @Override
    public void updateKeyData(KeyData keyData) {
        DbHelp.update(keyData);
    }

    @Override
    public KeyData getKeyDataByTagId(String tagId) {
        List<KeyData> list = DbHelp.getQueryByWhere(KeyData.class, "tagid", new String[]{tagId});
        if (list.size() > 0)
            return list.get(0);
        return null;
    }

    @Override
    public List<KeyData> getKeyDataByKeyBoxId(int keyboxId) {
        List<KeyData> list = DbHelp.getQueryByWhere(KeyData.class, "keybox",new String[]{String.valueOf(keyboxId)} );
        if (list.size() > 0)
            return list;
        return null;
    }

    @Override
    public void deleteAll() {
        DbHelp.deleteAll(KeyData.class);
    }

    @Override
    public KeyData getKeyDataBySerialnumber(String serialnumber) {
        List<KeyData> list = DbHelp.getQueryByWhere(KeyData.class, "serialnumber", new String[]{serialnumber});
        if (list.size() > 0)
            return list.get(0);
        return null;
    }

    @Override
    public List<KeyData> getAll() {
        List<KeyData> list = DbHelp.getQueryAll(KeyData.class);
         return  list;

    }

    @Override
    public List<KeyData> getAllKey() {
        return   DbHelp.getQueryByWhere(KeyData.class, "type", new String[]{"11"});
    }

    @Override
    public void delete(KeyData keyData) {
        DbHelp.delete(keyData);
    }
}
